import Vue from 'vue'
import VueRouter from 'vue-router'


Vue.use(VueRouter)

import login from '@/views/user/login'
import home from '@/views/home/home.vue'
import qa from '@/views/qa/qa'
import user from '@/views/user/user.vue'
import movie from '@/views/movie/movie'
import notfound from '@/views/notfound/notfound'
import bjzl from '@/views/user/bjzl.vue'
import xztx from '@/views/user/xztx.vue'
import article from '@/views/home/article.vue'
import seach from '@/views/home/seach.vue'
import searchjg from '@/views/home/searchjg.vue'

import { getToken } from '../utils/token'
const routes = [
  {
    path: "/",
    redirect: '/home'
  },
  {
    name: 'login',
    path: '/login',
    component:login
  },
  {
    name: 'home',
    path: '/home',
    component: home,
    meta: {
      showTab:true
    }
  }
  ,
  {
    name: 'user',
    path: '/user',
    component:user,
    meta: {
      showTab: true,
      isLogin:true
    }
  }
  ,
  {
    name: 'qa',
    path: '/qa',
    component:qa,
    meta: {
      showTab:true
    }
  }
  ,
  {
    name: 'movie',
    path: '/movie',
    component:movie,
    meta: {
      showTab:true
    }
  },
  {
    name: 'notfound',
    path: '/notfound',
    component:notfound
  },
  {
    name: 'bjzl',
    path: '/bjzl',
    component:bjzl
  },
  {
    name: 'xztx',
    path: '/xztx',
    component:xztx
  },
  {
    name: 'myarticle',
    path: '/article',
    component:article
  },
  {
    name: 'seach',
    path: '/seach',
    component:seach
  },
  {
    name: 'searchjg',
    path: '/searchjg',
    component:searchjg
  },
  {

    path: '*',
    redirect:'/notfound'
  }
]

//(1)先保存路由原型链中默认的跳转函数
const ispush = VueRouter.prototype.push
//(2)替换原型链中的push
VueRouter.prototype.push = function (a, b, c) {
  if (a | b) {
    return ispush.call(this,a,b,c)
  }
  return ispush.call(this,a).catch( err => {})
}

const router = new VueRouter({
  routes,
  mode:'history'
})

router.beforeEach((to, from, next) => {
  // 判断用户有没有登录
  if (to.meta.isLogin && !getToken()) {
    next('/login')
  } else {
    next()
  }
})

export default router
